constint N = 5e5 + 5; int n, s, cnt, fa[N], fc[N], dep[N], idx[N], sum[N]; bool used[N]; structedge {int to, val;}; vector<edge> nxt[N]; queue<int> q;
voidbfs(){ while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < (int)nxt[u].size(); i++) { if (dep[nxt[u][i].to] < 0) { dep[nxt[u][i].to] = dep[u] + nxt[u][i].val; fa[nxt[u][i].to] = u, fc[nxt[u][i].to] = nxt[u][i].val; q.push(nxt[u][i].to); } } } }
intfind_dia(int x){ memset(dep, -1, sizeof(dep)); q.push(x), dep[x] = 0; bfs(); for (int i = 1; i <= n; i++) { if (dep[x] < dep[i]) x = i; } return x; }
boolcheck(int x){ int l = 1, r = cnt; for (int y = x; l < cnt && y >= sum[l + 1] - sum[l]; l++) y -= sum[l + 1] - sum[l]; for (int y = x; r > 1 && y >= sum[r] - sum[r - 1]; r--) y -= sum[r] - sum[r - 1]; return sum[r] - sum[l] <= s; }
intmain(){ ios::sync_with_stdio(false); cin >> n >> s; for (int i = 1; i < n; i++) { int u, v, w; cin >> u >> v >> w; nxt[u].push_back((edge){v, w}), nxt[v].push_back((edge){u, w}); } int s = find_dia(1), t = find_dia(s), k = t; for (idx[++cnt] = k; k != s; k = fa[k]) { idx[++cnt] = fa[k], sum[cnt] = sum[cnt - 1] + fc[k]; } int l = 0, r = dep[t]; memset(dep, -1, sizeof(dep)); for (int i = 1; i <= cnt; i++) q.push(idx[i]), dep[idx[i]] = 0; bfs(); for (int i = 1; i <= n; i++) l = max(l, dep[i]); while (l < r) { int mid = (l + r) >> 1; if (!check(mid)) l = mid + 1; else r = mid; } cout << l << endl; return0; }